package com.fable.common.auth;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;

/**
 * Created by pyshift on 15/3/16.
 */
public class AudiUser implements java.io.Serializable {

    private static final ThreadLocal<AudiUser> authHolder = new ThreadLocal<AudiUser>();
    private static final long serialVersionUID = 3412261661747707739L;

    private Long userId;
    private String[] roles;
    private String loginName;
    private String phone;

    private String companyName;
    private boolean firstLogin;
    private String realName; // 真名

    private String nickName; // 花名 小二才会有
    // 当前用户可能有多个角色 临时存储进行当前操作使用的角色
    private transient String role;

    public static final String getCurrentRole() {
        AudiUser audiUser = authHolder.get();
        return audiUser.role;
    }

    public static final void setCurrentRole(String role) {
        AudiUser audiUser = authHolder.get();
        if (null != audiUser) {
            audiUser.role = role;
        }
    }

    public static final AudiUser getCurrentUser() {
        return authHolder.get();
    }

    public static final void setCurrentUser(AudiUser buyer) {
        authHolder.set(buyer);
    }

    public static final void cleanThreadLocal() {
        authHolder.remove();
    }

    /**
     * 创建匿名用户。
     */
    public AudiUser() {

    }

    /**
     * 创建用户。
     */
    public AudiUser(Long adminId) {
        this.userId = adminId;
    }


    public Long getUserId() {
        return userId;
    }

    public String[] getRoles() {
        if (roles == null) {
            return new String[0];
        }
        return roles;
    }

    public void setRoles(String[] roles) {
        this.roles = roles;
    }


    public boolean hasLoggedIn() {
        return userId != null;
    }


    public static boolean isHasRole(String roleName) {
        AudiUser audiUser = getCurrentUser();
        String[] roles1 = audiUser.getRoles();
        if (roles1 == null || roles1.length == 0) {
            return false;
        }
        for (String role : roles1) {
            if (roleName.equals(role)) {
                return true;
            }
        }
        return false;
    }


    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public boolean getFirstLogin() {
        return firstLogin;
    }

    public void setFirstLogin(Boolean firstLogin) {
        this.firstLogin = firstLogin;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}
